package com.selebash.scripts;

import com.selebash.Localization;
import com.selebash.ast.AbstractAst;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sad
 */
public class Script {

    private final Map<String, AbstractAst> functionsMap = new HashMap<String, AbstractAst>();
    private Localization localization;
    private final Map<String, String> properties = new HashMap<String, String>();
    private Script parentScript;

    public Script getParentScript() {
        return parentScript;
    }

    public void setParentScript(Script parentScript) {
        this.parentScript = parentScript;
    }

    public void addProperty(String name, String value) {
        properties.put(name.toLowerCase(), value);
    }

    public String getProperty(String value) {
        return properties.get(value.toLowerCase());
    }

    public String getTemplate() {
        return getProperty(localization.getString(Localization.TEMPLATE));
    }

    public String getModule() {
        return getProperty(localization.getString(Localization.MODULE));
    }

    public String getName() {
        return getProperty(localization.getString(Localization.SCRIPTNAME));
    }

    public String getFullName() {
        if (getModule() != null && !getModule().isEmpty()) {
            return getModule() + "." + getName();
        }
        return getName();
    }

    public String getDescription() {
        return getProperty(localization.getString(Localization.DESCRIPTION));
    }

    public Map<String, AbstractAst> getAstNodes() {
        return functionsMap;
    }

    public AbstractAst getFunctionNode(String functionName) {
        AbstractAst function = functionsMap.get(functionName);
        if (function != null) {
            return function;
        }

        if (parentScript == null) {
            return null;
        }

        function = parentScript.getFunctionNode(functionName);
        return function;
    }

    public Localization getLocalization() {
        return localization;
    }

    public void setLocalization(Localization localization) {
        this.localization = localization;
    }

}
